package com.ruoyi.web.controller.biz;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.JingquStatusConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BsJingquTypeEnum;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BsJingqu;
import com.ruoyi.system.domain.excel.JingquExcel;
import com.ruoyi.system.domain.ro.BsJingquSearchPage;
import com.ruoyi.system.service.BsJingquService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.ruoyi.web.controller.biz
 * @Author: HanWL
 * @CreateTime: 2023-10-16 15:01
 * @Version: 1.0
 **/
@RestController
@Api(tags = "景区接口")
@RequestMapping("/bs/jingqu")
@Slf4j
public class BsJingquController extends BaseController {

    @Resource
    private BsJingquService bsJingquService;

    @ApiOperation(value = "景区分页搜索", tags = {"景区接口", "游客端-智能搜索"})
    @GetMapping("page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态(0开放，1关闭)"),
            @ApiImplicitParam(name = "jingquName", value = "景区名称-模糊查询"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    public TableDataInfo<BsJingqu> page(BsJingquSearchPage param) {
//        LambdaQueryWrapper<BsJingqu> lqw = new LambdaQueryWrapper<>();
//        if (StringUtils.isNotNull(status)) {
//            lqw.eq(BsJingqu::getJingquStatus, status);
//        }
//        if (StringUtils.isNotEmpty(startTime)) {
//            lqw.gt(BsJingqu::getJingquOpenTime, startTime);
//        }
//        if (StringUtils.isNotNull(endTime)) {
//            lqw.lt(BsJingqu::getJingquOpenTime, endTime);
//        }
//        lqw.eq(BsJingqu::getDelFlag, Constants.LOGIC_NOT_DELETE);
//        lqw.like(StrUtil.isNotBlank(jingquName), BsJingqu::getJingquName, jingquName);
        startPage();
//        List<BsJingqu> list = bsJingquService.list(lqw);
        List<BsJingqu> list = bsJingquService.listPage(param);
        return getDataTable(list);
    }

    @ApiOperation("新增景区")
    @PostMapping("/save")
    public AjaxResult<Void> save(@RequestBody BsJingqu bsJingqu) {
        boolean result = bsJingquService.saveAndSaveJingDian(bsJingqu);
        return toAjax(result);
    }

    @ApiOperation("根据景区ID获取景区详情")
    @GetMapping("getDetailsByJingquId/{jingquId}")
    public AjaxResult<BsJingqu> getDetailsByJingquId(@PathVariable Long jingquId) {
        BsJingqu bsJingqu = bsJingquService.getJingQuDetails(jingquId);
        return AjaxResult.success(bsJingqu);
    }

    @ApiOperation("获取所有景区列表")
    @GetMapping("/getList")
    public AjaxResult<List<BsJingqu>> getList() {
        LambdaQueryWrapper<BsJingqu> lqw = new LambdaQueryWrapper<BsJingqu>()
                .eq(BsJingqu::getDelFlag, Constants.LOGIC_NOT_DELETE);
        return AjaxResult.success(bsJingquService.list(lqw));
    }

    @ApiOperation("获取所有景区列表(带上景点集合)")
    @GetMapping("/getListWithJingDian")
    public AjaxResult<List<BsJingqu>> getListWithJingDian() {
        List<BsJingqu> list = bsJingquService.listWithJingDian();
        return AjaxResult.success(list);
    }

    @ApiOperation("修改景区信息")
    @PutMapping("/update")
    public AjaxResult<Void> update(@RequestBody BsJingqu bsJingqu) {
        boolean result = bsJingquService.updateAndupdateJingDian(bsJingqu);
        return toAjax(result);
    }

    @ApiOperation("查询景区当前预约状态")
    @GetMapping("/getJingquStatus/{jingquId}")
    public AjaxResult<Integer> getJingquStatus(@PathVariable Long jingquId) {
        BsJingqu entity = bsJingquService.getById(jingquId);
        return AjaxResult.success("查询成功", entity.getJingquStatus());
    }

    @ApiOperation("预约开关")
    @GetMapping("/switch/{jingquId}")
    public AjaxResult<Void> stop(@PathVariable Long jingquId) {
        BsJingqu entity = bsJingquService.getById(jingquId);
        if (entity.getJingquStatus().equals(JingquStatusConstants.CLOSE)) {
            entity.setJingquStatus(JingquStatusConstants.OPEN);
        } else {
            entity.setJingquStatus(JingquStatusConstants.CLOSE);
        }
//        entity.setJingquStatus(JingquStatusConstants.CLOSE);
        return toAjax(bsJingquService.updateById(entity));
    }

    @ApiOperation("景区导出")
    @GetMapping("export")
    public AjaxResult<?> export(Integer status, String startTime, String endTime) {
        LambdaQueryWrapper<BsJingqu> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(status)) {
            lqw.eq(BsJingqu::getJingquStatus, status);
        }
        if (StringUtils.isNotEmpty(startTime)) {
            lqw.gt(BsJingqu::getJingquOpenTime, startTime);
        }
        if (StringUtils.isNotNull(endTime)) {
            lqw.lt(BsJingqu::getJingquOpenTime, endTime);
        }
        lqw.eq(BsJingqu::getDelFlag, Constants.LOGIC_NOT_DELETE);
        List<BsJingqu> list = bsJingquService.list(lqw);
        ExcelUtil<JingquExcel> util = new ExcelUtil<>(JingquExcel.class);
        return util.exportExcel(ConvertUtil.sourceToTarget(list, JingquExcel.class), "景区信息");
    }

    @ApiOperation("删除景区")
    @DeleteMapping("/delete/{ids}")
    public AjaxResult<?> delete(@PathVariable Long[] ids) {
        boolean result = bsJingquService.logicRemove(Arrays.asList(ids));
        return toAjax(result);
    }

    @ApiOperation("景区类型")
    @GetMapping("/type")
    public AjaxResult<?> type(){
        List<Map<String, Object>> typeAndNameMap = BsJingquTypeEnum.getTypeAndNameMap();
        return AjaxResult.success(typeAndNameMap);
    }

}
